//
//  main.c
//  pr_fun1
//
//  Created by chenyh on 2021/1/22.
//

#include <stdio.h>
#include <stdbool.h>
#include "dlist_node.h"
#include "iterator.h"

typedef struct _Validator {
    bool(*const validate)(struct _Validator *pThis,int value);
}Validator;

typedef struct {
    Validator isa;
    const int min;
    const int max;
}RangeValidator;


typedef  struct {
    const int min;
    const int max;
}Range;


typedef struct {
    bool isEven;
}OddEven;

bool validateRange(void * pData,int value);
bool validateOddEven(void *pData,int value);

int *max(int *p1,int *p2)
{
    if (*p1 > *p2) {
        return  p1;
    }else
        return p2;
}

bool validateRange(void *pData,int value)
{
    Range *pRange = (Range *)pData;
    return pRange->min <= value && value <= pRange->max;
}
bool validateOddEven(void *pData,int value)
{
    OddEven *pOddEven = (OddEven *)pData;
    return (!pOddEven->isEven && (value % 2)) || (pOddEven->isEven && !(value%2));
}





typedef struct _dlist_int {
    dlist_node_t node;  //包含链表结点
    int data;           //int类型数据
}dlist_int_t;

int list_node_process(void *p_arg,iterator_t p_node)
{
    printf("%d \r\n",((dlist_int_t *)p_node)->data);
    return 0;
}

static int __compare(iterator_t it1,iterator_t it2) {
    return ((dlist_int_t *)it1)->data-((dlist_int_t *)it2)->data;
}

static void __swap(iterator_t it1,iterator_t it2)
{
    int data = ((dlist_int_t *)it2)->data;
    ((dlist_int_t *)it2)->data = ((dlist_int_t *)it1)->data;
    ((dlist_int_t *)it1)->data = data;
}




static void __visit(void *p_arg,iterator_t it)
{
    printf("%d  ",*(int *)it);
}

static int __array_compare(iterator_t *it1,iterator_t it2)
{
    return *(int *)it1-*(int *)it2;
}
static void __array_swap(iterator_t *it1,iterator_t it2)
{
    int data = *(int *)it2;
    *(int *)it2 = *(int *)it1;
    *(int *)it1 = data;
}

int main(void)
{
    iterator_if_t iterator_if;
    int a[] = {5,4,3,2,1};
    array_iterator_if_get(&iterator_if);
    
    printf("\nBefore bubble sort:\n");
    iter_foreach(&iterator_if, a, a+4, __visit, NULL);
    
    iter_sort(&iterator_if, a, a+4, __array_compare, __array_swap);
    
    printf("\n After bubble sort:\n");
    iter_foreach(&iterator_if, a, a+4, __visit, NULL);
    
    
    
    
}

/*
int main(int argc,char *argv[])
{
    iterator_if_t iterator_if;
    dlist_head_t  head;     //定义链表头结点
    dlist_int_t   node[5];  //定义5个结点
    int            i;
    dlist_init(&head);
    for (i = 0; i < 5; i++) {
        node[i].data = 5 - i;
        dlist_add_tail(&head, &(node[i].node));
    }
    dlist_iterator_if_get(&iterator_if);
    printf("\n Befor bubble sort:\n");
    
    iter_foreach(&iterator_if, dlist_begin_get(&head), dlist_end_get(&head), list_node_process, NULL); //打印排序前的情况
    
    dlist_foreach(&head, __compare, __swap);
//    iter_sort(&iterator_if, dlist_begin_get(&head), dlist_end_get(&head), __compare, __swap);
    printf("\nAfter bubble sort:\n");
    iter_foreach(&iterator_if, dlist_begin_get(&head), dlist_end_get(&head), list_node_process, NULL);; //打印排序后的情况
    return  0;;
}
*/
/*
int main(void)
{
    dlist_head_t head; //定义链表头结点
    dlist_int_t node1,node2,node3;
    dlist_init(&head);
    
    node1.data = 1;
    dlist_add_tail(&head, &(node1.node));
    
    node2.data = 2;
    dlist_add_tail(&head, &(node2.node));
    
    node3.data = 3;
    dlist_add_tail(&head, &(node3.node));
    
    dlist_del(&head, &(node2.node));
    dlist_foreach(&head, list_node_process, NULL); //遍历链表，用户参数为NULL
    return 0;
}
*/
/*

int main(int argc, const char * argv[]) {
    int *p, a,b;
    a=1;b=2;
    p = max(&a, &b);
    
    Range pRange = {10,20};
    
    printf("%d",validateRange(&pRange, 15));
    OddEven oddEven;
    oddEven.isEven = true;
    printf("%d",validateOddEven(&oddEven, 10));
    
    RangeValidator rangeValidator;
    Validator *pThis = &rangeValidator.isa;
//    Validator *pThis = (Validator *)&rangeValidator;
    
    printf("%d\n",*p);
    return 0;
}
*/
